﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace ClientAppraise.Converters;

public class BitToBoolConverter : IMultiValueConverter
{
    ulong Data;
    uint pos;
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values == default|| values.Length<2)
        {
            Data = 0; pos = 0;
            return false;
        }

        if(values[0]==default|| values[1] == default)
        {
            Data = 0; pos = 0;
            return false;
        }

        if (ulong.TryParse(values[0].ToString(), out var a)) {
            Data = 0; pos = 0;
            return false;
        }

        if (uint.TryParse(values[1].ToString(), out var b))
        {
            Data = 0; pos = 0;
            return false;
        }

        Data = a;
        pos = b;

        return Data & (ulong)Math.Pow(2, pos);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        var b = (bool)value;

        var objects = new object[2] { Data, pos };

        if (b)
        {
            objects[0] = (Data | (ulong)Math.Pow(2, pos));
        }
        else
        {
            objects[0] = (Data & (~(ulong)Math.Pow(2, pos)));
        }


        return objects;
    }
}
